package com.guet.careapp.utils;

import java.util.List;

/**
 * 分页用的一页的信息对象
 *
 * @author cx
 */
public class PageBean {

  // 传递的参数或配置的值
  private int currentPage; // 当前页
  private int pageSize; // 每页显示的记录数
  private int showPageCount = 8;// 页面控制器显示的页码数量
  // 查询数据库
  private int recordCount; // 总记录数
  private List recordList; // 本页的数据列表

  // 计算出来的
  private int pageCount; // 总页数
  private int beginPageIndex; // 页面列表的开始索引
  private int endPageIndex; // 页面列表的结束索引

  /**
   * 只接受前4个必要的属性的值，会自动的计算出后3个属性的值
   */
  public PageBean(int currentPage, int pageSize, int recordCount, List recordList) {
    this.currentPage = currentPage;
    this.pageSize = pageSize;
    this.recordCount = recordCount;
    this.recordList = recordList;
    int halfShowPageCount = (int) showPageCount / 2;
    // 计算pageCount
    pageCount = (recordCount + pageSize - 1) / pageSize;

    // 计算begPageIndex和endPageIndex
    // a, 总页数不超过showPageCount页，就全部显示
    if (pageCount <= showPageCount) {
      beginPageIndex = 1;
      if (pageCount < 0) {
        endPageIndex = 1;
        pageCount = 1;
      } else {
        endPageIndex = pageCount;
      }
    } else {// b, 总页数超过了showPageCount页，就显示当前页附近的共showPageCount个页码（前2个 + 当前页
      // + 后2个）

      // 如果显示页数为奇数，则当前页前后相等
      if (showPageCount % 2 == 1) {
        beginPageIndex = currentPage - halfShowPageCount; //

        endPageIndex = currentPage + halfShowPageCount; //
      } else {// 如果是偶数页，则前少后多
        System.out.println("偶数");
        beginPageIndex = currentPage - halfShowPageCount + 1; //
        // showPageCount
        endPageIndex = currentPage + halfShowPageCount; //
      }
      // 当前面不足showPageCount/2个页码时，就显示前10页
      if (beginPageIndex < 1) {
        beginPageIndex = 1;
        endPageIndex = this.showPageCount;
      }
      // 当后面不足showPageCount个页码时，就显示后10页
      else if (endPageIndex > pageCount) {
        beginPageIndex = beginPageIndex - (halfShowPageCount - (pageCount - currentPage));
        endPageIndex = pageCount;
      }
    }
  }

  public int getCurrentPage() {
    return currentPage;
  }

  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }

  public int getPageCount() {
    return pageCount;
  }

  public void setPageCount(int pageCount) {
    this.pageCount = pageCount;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public int getRecordCount() {
    return recordCount;
  }

  public void setRecordCount(int recordCount) {
    this.recordCount = recordCount;
  }

  public List getRecordList() {
    return recordList;
  }

  public void setRecordList(List recordList) {
    this.recordList = recordList;
  }

  public int getBeginPageIndex() {
    return beginPageIndex;
  }

  public void setBeginPageIndex(int beginPageIndex) {
    this.beginPageIndex = beginPageIndex;
  }

  public int getEndPageIndex() {
    return endPageIndex;
  }

  public void setEndPageIndex(int endPageIndex) {
    this.endPageIndex = endPageIndex;
  }

  @Override
  public String toString() {
    return "PageBean [currentPage=" + currentPage + ", pageSize=" + pageSize + ", recordCount="
        + recordCount + ", pageCount=" + pageCount
        + ", beginPageIndex=" + beginPageIndex + ", endPageIndex=" + endPageIndex
        + "\n recordList=\n" + Info.getStrList(recordList) + "]";
  }

}
